<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" />
		<meta content="yes" name="apple-mobile-web-app-capable" />
		<meta content="black" name="apple-mobile-web-app-status-bar-style">
		<meta content="telephone=no" name="format-detection" />
		<meta content="emai=no" name="format-detection" />
		<link rel="stylesheet" type="text/css" href="<?php echo STATIC_HOST?>shared/css/shared.css" />
		<link rel="stylesheet" type="text/css" href="<?php echo STATIC_HOST?>coupon/css/list.css" />
		<script type="text/javascript" src="<?php echo STATIC_HOST?>shared/js/zepto-1.1.6.js"></script>
		<script type="text/javascript" src="<?php echo STATIC_HOST?>shared/js/md5.js"></script>
		<title>优惠券</title>
	</head>

	<body>
		<header>
			<ul class="flex-wrapper flex-align-center">
				<li class="flex-1 <?php if($type=='0') echo 'active' ?>">
					<a data-href="?type=0">未使用(<?php echo $totalList['0']?$totalList['0']:0 ?>)</a>
				</li>
				<li class="flex-1 <?php if($type=='1,2,3') echo 'active' ?>">
					<a data-href="?type=1,2,3">使用记录(<?php echo ($totalList['1']+$totalList['2']+$totalList['3'])?$totalList['1']+$totalList['2']+$totalList['3']:0 ?>)</a>
				</li>
				<li class="flex-1 <?php if($type=='4') echo 'active' ?>">
					<a data-href="?type=4">已过期(<?php echo $totalList['4']?$totalList['4']:0 ?>)</a>
				</li>
			</ul>
		</header>
		<div class="wrapper">
			<ul class="list">
				<!--s1:赠送中；s2:已赠送；s3已使用；s4已过期-->
				<?php if(!empty($list)){ ?>
				<?php foreach($list as $k => $v){ ?>
					<li class="flex-wrapper flex-align-center s<?php echo $v['coupons_status']?>"  data-coupon="<?=$v['couponsid']?>" data-money="<?= $v['coupons_money']?>" data-amount="<?=$v['is_back_send_total']?>">
						<div class="flex-left">
							<p class="price">&yen;<span><?php echo $v['coupons_money']?></span></p>
							<p class="desc">满<?php echo $v['coupons_full_money']?>元可用</p>
						</div>
						<div class="flex-1 info">
							<p class="title"><?php echo $v['coupons_name']?><span class="number"><?php echo $v['total']?>张</span></p>
							<p><?php echo date('Y.m.d H:i',strtotime($v['use_start_time'])).' - '.date('Y.m.d H:i',strtotime($v['use_end_time'])) ?></p>
							<?php if($type=='0'){?>
								<p class="action">
									<?php if($v['is_back_send_total']!=0){ ?>
										<a  data-href="javascript:void(0)" class="btn btn-blue btn-send">转赠好友</a>
									<?php } ?>
									<a data-href="/product/detail?id=<?php echo $v['product_id'] ?>" class="btn btn-orange">立即使用</a>
								</p>
							<?php } ?>

							<?php if($type=='1,2,3'){?>
								<?php if($v['coupons_status']=='2'){?>
									<p class="action">
										<a url="/coupon/undo" couponsid="<?=$v['couponsid']?>" onclick="undo(this);"   class="btn btn-orange">撤销转赠</a>
									</p>
								<?php } ?>
							<?php } ?>
						</div>
					</li>
				<?php } ?>
				<?php }else{ ?>
					<p class="no-result">
						暂没有可用的优惠券~
					</p>
				<?php } ?>
			</ul>
		</div>
		

		<div class="hide popup-amount">
<!--			优惠券的id-->
			<input type="hidden" name="hiddenCouponID" value=""/>
<!--			md5的编码-->
			<input type="hidden" name="hiddenUniqueCode" value=""/>
<!--			选择的个数-->
			<input type="hidden" name="hiddenAmount" value=""/>
<!--			金额-->
			<input type="hidden" name="hiddenMoney" value=""/>
			<input type="hidden" name="hiddenMax" value=""/>
			<span class="close" data-href="javascript:zsmm.Popup.clear();"><img class="fullScreen" src="<?=STATIC_HOST?>coupon/img/i_close.png" /></span>
			<form action="" method="get">
				选择转赠的优惠券数量<br/>（最多<span id="max_amount"></span>张）
				<p class="amount-select">
					<button id="btn-add" onclick="if(amount.value>1) amount.value--"></button>
					<input type="number" name="amount" value="1" min="1"/>
					<button id="btn-minus" onclick="amount.value++"></button>
				</p>
				<a class="btn btn-red" id="btn-submit" onclick="submitCoupon($(this).siblings().find('input'))">确 认</a>
			</form>
		</div>

		<div class="hide popup-share" onclick="zsmm.Popup.clear()">
			<img class="fullScreen" src="<?=STATIC_HOST?>coupon/img/d_share.png" />
		</div>


		<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
		<script type="text/javascript" src="<?= STATIC_HOST?>shared/js/shared.js"></script>
		<script type="text/javascript" src="<?= STATIC_HOST?>coupon/js/list.js"></script>
		<script type="text/javascript">
			wx.config({
				debug: false,
				appId: '<?= $js_config['appId']?>',
				timestamp: <?= $js_config['timestamp']?>,
				nonceStr: '<?= $js_config['noncestr']?>',
				signature: '<?= $js_config['signature']?>',
				jsApiList: [
					'onMenuShareTimeline',
					'onMenuShareAppMessage'
				]
			});
			//服务器url
			var service_url="<?=$base_url?>";
			//点击确定数量
			function submitCoupon(obj) {
				var count = obj.val();
				var money=$("input[name='hiddenMoney']").val();
				var can_do=get_can_release_number($("input[name='hiddenCouponID']").val(),count);
				if(valicateNumber(obj)==true){
					if(can_do){
						$("input[name='hiddenAmount']").val(count);
						zsmm.Popup.clear();
						zsmm.Popup.New($('.popup-share'));
						var md5_code=generateID();
						$('input[name=hiddenUniqueCode]').val(md5_code);
						change_wx_config('送你一张a1元芝妈优选优惠券',service_url+'/coupon/receive?coupon_code='+md5_code,'送你一张a2元芝妈优选优惠券，为您的钱包君省钱呦',money);
					}else{
						alert('数量超出限制');
					}
				}
				//设定微信分享文案、链接
			}

			//获取可以发放的数量
			function  get_can_release_number(coupon_id,count){
				return true;
				$.post('/coupon/check-number',{'coupon_id':coupon_id},function ($data) {
					ffffff= parseInt($data.number)>=parseInt(count);
				},'json');
			}
			//修改分享js配置
			function change_wx_config(title,link,description,money){
				wx.ready(function () {
					wx.onMenuShareTimeline({
						title: title.replace(/a1/g,money),
						link: link, // 分享链接
						imgUrl: 'http://test.static.zhishimama.com/coupon/img/icon.jpg', // 分享图标
						success: function () {
							// 用户确认分享后执行的回调函数
							send_ajax('/coupon/share');
						},
						cancel: function () {
							// 用户取消分享后执行的回调函数
							zsmm.Popup.clear();
						}
					});

					wx.onMenuShareAppMessage({
						title: title.replace(/a1/g,money),
						desc: description.replace(/a2/g,money), // 分享描述
						link: link, // 分享链接
						imgUrl: 'http://test.static.zhishimama.com/coupon/img/icon.jpg', // 分享图标
						type: 'link', // 分享类型,music、video或link，不填默认为link
						dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
						success: function () {
							// 用户确认分享后执行的回调函数
							send_ajax('/coupon/share');
						},
						cancel: function () {
							// 用户取消分享后执行的回调函数
							zsmm.Popup.clear();
						}
					});
				});
			}


			//请求分享的接口
			function	send_ajax(share_url){
				$.post(
					share_url,
					{
						'coupon_id':$("input[name='hiddenCouponID']").val(),
						'number':$("input[name='hiddenAmount']").val(),
						'unique_code':$("input[name='hiddenUniqueCode']").val()
					},
					function ($data) {
						if($data.status){
							//调用成功
							window.location.reload();
						}else{
							//调用失败
							alert($data.msg);
						}
					},
					'json'
				);
			}

		</script>
	</body>

	<script>
		//撤销的操作
		function	undo(obj){
			var coupons_id=$(obj).attr('couponsid');
			if(coupons_id){
				$(obj).removeAttr('onclick');
				$.post($(obj).attr('url'),{'coupon_id':coupons_id},function ($data) {
					if($data.status){
						zsmm.Toast.New($data.msg,1);
						setTimeout(function(){
							window.location.reload();
						},1);
					}else{
						zsmm.Toast.New($data.msg,1);
					}
					$(obj).attr('onclick','undo(this)');
				},'json');
			}else{
				return false;
			}
		}

	</script>
</html>